import scrapy
import json

class FypostSpider(scrapy.Spider):
    # 百度翻译 post请求
    name = 'fypost'
    allowed_domains = ['https://fanyi.baidu.com/sug']
    # 下边的请求只适用于 GET
    # start_urls = ['https://fanyi.baidu.com/sug']
    #
    # def parse(self, response):
    #     pass

    # POST 请求定义
    def start_requests(self):
        url = 'https://fanyi.baidu.com/sug'

        data = {
            'kw': 'see'
        }
        # scrapy 发起post请求
        yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse_second)

    # POST 请求解析结果
    def parse_second(self, response):

        content = response.text
        obj = json.loads(content)
        print(obj)
